//
//  JSBaseTableViewController.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/14.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSBaseTableViewController.h"

@interface JSBaseTableViewController ()

@end

@implementation JSBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initTableView{
    [super initTableView];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    _isHidenHeaderRefresh = YES;
    _isHidenFooterRefresh = YES;
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    
}
- (void)headerRefresh{
    self.page = 1;
    [self.tableView.mj_footer endRefreshing];
    self.params[@"page"] = @(self.page);
    [self loadData:YES];
}
- (void)footerRefresh{
    self.page ++;
    self.params[@"page"] = @(self.page);
    [self.tableView.mj_header endRefreshing];
    [self loadData:NO];
    
}

-(void)loadData:(BOOL)isNewData{
    if (!self.mouth) {
//        JSAlertWithMessge(@"方法名为空");
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        return;
    }
    self.params[@"pagenum"] = @(self.pageNumber);
    JS_WeakSelf(self)
    [JSNetManager js_courrentProjectRequestWithMouth:self.mouth cache:NO ifShowFinishStatu:NO parameters:self.params successBlock:^(id response) {
        [weakself.tableView.mj_header endRefreshing];
        [weakself.tableView.mj_footer endRefreshing];
        if (isNewData) {
            NSMutableArray *temp = weakself.dataSource;
            [weakself.dataSource removeAllObjects];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [temp class];
            });
            //            weakself.dataSource = nil;
            if (weakself.response) {
                weakself.response(response, YES);
            }
        }else{
            if (weakself.response) {
                weakself.response(response, NO);
            }
        }
        
    } failureBlock:^(NSError *error) {
        [weakself.tableView.mj_header endRefreshing];
        [weakself.tableView.mj_footer endRefreshing];
        if (!isNewData) {
            weakself.page --;
        }
    }];
}



- (void)setIsHidenFooterRefresh:(BOOL)isHidenFooterRefresh{
    _isHidenFooterRefresh = isHidenFooterRefresh;
    if (isHidenFooterRefresh) {
        self.tableView.mj_footer = nil;
    }else{
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    }
}
- (void)setIsHidenHeaderRefresh:(BOOL)isHidenHeaderRefresh{
    _isHidenHeaderRefresh = isHidenHeaderRefresh;
    if (_isHidenHeaderRefresh) {
        self.tableView.mj_header = nil;
    }else{
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
        // 自动改变透明度
        self.tableView.mj_header.automaticallyChangeAlpha = YES;
    }
    
}
#pragma mark - DZNEmptyDataSetSource
// 空白页图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"noData"];
}
// 标题文本，富文本样式
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"暂无数据";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: [UIColor JS_ColorWithR:222 G:222 B:222]};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}


// 是否 允许上下滚动，默认NO
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}
//MARK:懒加载
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = @[].mutableCopy;
    }
    return _dataSource;
}
- (NSMutableDictionary *)params{
    if (!_params) {
        _params = @{}.mutableCopy;
    }
    return _params;
}

- (nullable UIImage *)navigationBarBackgroundImage{
    return [UIImage qmui_imageWithColor:UIColorWhite];
}
//设置当前导航栏是否需要使用自定义的 push 时候 transition 效果，默认返回NO，须足这里配置设置YES。
- (BOOL)shouldCustomNavigationBarTransitionWhenPushAppearing {
    return YES;
}
//设置当前导航栏是否需要使用自定义的 pop  时候 transition 效果，默认返回NO，须足这里配置设置YES。
- (BOOL)shouldCustomNavigationBarTransitionWhenPopAppearing {
    return YES;
}
//- (void)hideTableHeaderViewInitialIfCanWithAnimated:(BOOL)animated force:(BOOL)force {
//    if (self.tableView.tableHeaderView && [self shouldHideTableHeaderViewInitial] && (force || !self.hasHideTableHeaderViewInitial)) {
//        CGPoint contentOffset = CGPointMake(self.tableView.contentOffset.x, self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.tableHeaderView.frame));
//        [self.tableView setContentOffset:contentOffset animated:animated];
//        self.hasHideTableHeaderViewInitial = YES;
//    }
//}
@end
